<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /*
            如果非二叉搜索树，只是普通的二叉树，那么可以用前序遍历
            二叉搜索树的话用中序即可，得到一个递增子序列
            */
            var findMode = function (root) {
                let res = []
                function inOrder(root) {
                    if (!root) return null
                    inOrder(root.left)
                    res.push(root.val)
                    inOrder(root.right)
                }
                inOrder(root)
                let map = new Map()
                res.forEach((item) => {
                    if (!map.has(item)) {
                        map.set(item, 1)
                    } else {
                        map.set(item, map.get(item) + 1)
                    }
                })
                let count = 0
                let v = []
                for (let [key, value] of map) {
                    if (count == value) {
                        v.push(key)
                    }
                    if (count < value) {
                        v = []
                        count = value
                        v.push(key)
                    }
                }
                return v
            }
        </script>
    </body>
</html>
